//
//  RBRequestHM.m
//  Culture
//
//  Created by Lan on 2017/12/26.
//  Copyright © 2017年 SummerTea. All rights reserved.
//

#import "RBRequestHM.h"

@implementation RBRequestHM

- (void)requestWithParam: (void(^)(NSMutableDictionary *dicM))paramSetupBlock
                     log: (BOOL)log
                    post: (BOOL)post
       cancelLastRequest: (BOOL)cancelLastRequest
               fileParam: (NSDictionary *)fileParam
            fileProgress: (void(^)(NSProgress * uploadProgress))progressBlock
           responseClass: (Class)responseClass
                pathName: (NSString *)pathName
              completion: (void(^)(id response))responseBlock
{
    self.paramDicM = nil;
    if (paramSetupBlock)
    {
        paramSetupBlock(self.paramDicM);
    }
    NSString *path = [RBURLConfig pathWithService:0 fileName:pathName];
    if (fileParam.allKeys.count == 4)
    {
        [self.manager postWithPath:path parameters:self.paramDicM fileData:fileParam[kRBRequestData] name:fileParam[kRBRequestName] fileName:fileParam[kRBRequestFileName] mimeType:fileParam[kRBRequestMime] responseDataClass:responseClass cancelRequest:cancelLastRequest progress:progressBlock completion:^(NSHTTPURLResponse *response, id responseObject, NSError *error) {
            responseBlock(responseObject);
        }];
    }
    else
    {
        if (post)
        {
            [self.manager postWithURLPath: path parameters:self.paramDicM responseDataClass:responseClass cancelRequest:cancelLastRequest cacheGetPolicy:0 cacheGetBlock:nil completionBlock:^BOOL(NSHTTPURLResponse *response, id responseObject, NSError *error) {
                responseBlock(responseObject);
                return NO;
            }];
        }
        else
        {
            [self.manager getWithURLPath: path parameters:self.paramDicM responseDataClass:responseClass cancelRequest:cancelLastRequest cacheGetPolicy:0 cacheGetBlock:nil completionBlock:^BOOL(NSHTTPURLResponse *response, id responseObject, NSError *error) {
                responseBlock(responseObject);
                return NO;
            }];
        }
    }
}

#pragma mark - Getter

- (RBHTTPManager *)manager
{
    if (!_manager)
    {
        self.manager = [RBHTTPManager new];
    }
    return _manager;
}

- (NSMutableDictionary *)paramDicM
{
    if (!_paramDicM)
    {
        self.paramDicM = [NSMutableDictionary dictionary];
    }
    return _paramDicM;
}

@end
